package controllers.Heuristics;

import core.game.Observation;
import core.game.StateObservation;
import ontology.Types;
import tools.Vector2d;

import java.io.ObjectStreamException;
import java.util.ArrayList;
import java.util.Vector;

/**
 * Created with IntelliJ IDEA.
 * User: ssamot
 * Date: 11/02/14
 * Time: 15:44
 * This is a Java port from Tom Schaul's VGDL - https://github.com/schaul/py-vgdl
 */
public class WinScoreHeuristic extends StateHeuristic {

    private static final double HUGE_NEGATIVE = -1000.0;
    private static final double HUGE_POSITIVE =  1000.0;

    double initialNpcCounter = 0;

    public WinScoreHeuristic(StateObservation stateObs) {

    }

    public double evaluateState(StateObservation stateObs) {
        boolean gameOver = stateObs.isGameOver();
        Types.WINNER win = stateObs.getGameWinner();

        double gameScore = 0;
        double healthPoints = 0;
        double rawScore = 0;
        try {
            gameScore = stateObs.getGameScore();
        } catch (Exception NullPointerException) {;
        }
        try {
            healthPoints = stateObs.getAvatarHealthPoints();
        } catch (Exception NullPointerException) {;
        }
        int portal_x = 0, portal_y = 0;
        if (stateObs.getPortalsPositions()!=null) {
            for (ArrayList<Observation> l : stateObs.getPortalsPositions()) {
                for (Observation o : l) {
                    portal_x = (int)o.position.x/28;
                    portal_y = (int)o.position.y/28;
                    break;
                }
            }
        }

        Vector2d avatar_pos = stateObs.getAvatarPosition();
        int avatar_x = (int) avatar_pos.x/28;
        int avatar_y = (int) avatar_pos.y/28;
        rawScore += -1.5*(16-avatar_y)*Math.abs(portal_x-avatar_x) + 3*20*(portal_y-avatar_y);

        rawScore += 100*gameScore + 400*healthPoints;

        if(gameOver && win == Types.WINNER.PLAYER_LOSES) return HUGE_NEGATIVE;
        if(gameOver && win == Types.WINNER.PLAYER_WINS) return HUGE_POSITIVE;
        return rawScore;
    }


}


